package org.edu.modules.sbzc.fund.service.impl;

import cn.hutool.core.convert.Convert;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.sbzc.fund.dto.VeSbzcZcAssetDto;
import org.edu.modules.sbzc.fund.mapper.VeSbzcZcPurchaseMapper;
import org.edu.modules.sbzc.fund.entity.VeSbzcZcPurchase;
import org.edu.modules.sbzc.fund.dto.VeSbzcZcPurchaseDto;
import org.edu.modules.sbzc.fund.service.VeSbzcZcAssetService;
import org.edu.modules.sbzc.fund.service.VeSbzcZcPurchaseService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 实现类
 */
@Service
public class VeSbzcZcPurchaseServiceImpl extends BaseServiceImpl<VeSbzcZcPurchase, VeSbzcZcPurchaseMapper> implements VeSbzcZcPurchaseService {

    @Autowired
    private VeSbzcZcAssetService veSbzcZcAssetService;

    public IPage<VeSbzcZcPurchaseDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeSbzcZcPurchaseDto findDtoById(String id) {
        VeSbzcZcPurchase entity = super.findById(id);
        VeSbzcZcPurchaseDto dto = ConvertUtils.convertToDto(entity, VeSbzcZcPurchaseDto.class);
        return dto;
    }

    /**
    * 新增或者修改
    */
    @Override
    public void insertOrUpdate(VeSbzcZcPurchaseDto dto,Boolean isAdd){
        if(isAdd){
            super.insert(dto);
        }else{
            super.update(dto);
        }
        VeSbzcZcAssetDto veSbzcZcAssetDto = veSbzcZcAssetService.findDtoById(Convert.toStr(dto.getAssetId()));
        int oldCount = veSbzcZcAssetDto.getCount();
        int addCount = dto.getCount();
        veSbzcZcAssetDto.setCount(oldCount+addCount);
        //更新资产数量
        veSbzcZcAssetService.save(veSbzcZcAssetDto);
    }

}
